package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import liumengbaio.lolzhangyoubao.Activity.utils.GoodView;
import liumengbaio.lolzhangyoubao.R;

/**
 * 视频播放界面
 * Created by lanouhn on 16/8/27.
 */
public class VideoThird_bodyActivity extends AppCompatActivity {
    private WebView wb;
    private String url, third_url;
    private ImageView back, more, good, goodChecked, bad, badChecked;
    private PopupWindow mPopupWindow;
    private GoodView mGoodView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_third_body);
        mGoodView = new GoodView(this);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        wb = (WebView) findViewById(R.id.video_third_wb);
        back = (ImageView) findViewById(R.id.video_third_badyBack);
        more = (ImageView) findViewById(R.id.video_third_badyMore);
        good = (ImageView) findViewById(R.id.video_good);
        bad = (ImageView) findViewById(R.id.video_bad);
        goodChecked = (ImageView) findViewById(R.id.video_goodChecked);
        badChecked = (ImageView) findViewById(R.id.video_badChecked);
    }

    private void initData() {
        WebSettings mWebSettings = wb.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        Intent intent = getIntent();
        third_url = intent.getStringExtra("third_url");
        wb.loadUrl(third_url);


    }

    private void initEvent() {

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        wb.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                WebSettings mWebSettings = wb.getSettings();
                mWebSettings.setJavaScriptEnabled(true);
                Intent intent = getIntent();
                url = intent.getStringExtra("video_url");
                wb.loadUrl(url);
                return false;
            }
        });

        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view = getLayoutInflater().inflate(R.layout.video_popwindow, null);
                mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                mPopupWindow.showAsDropDown(more);
                view.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (mPopupWindow != null && mPopupWindow.isShowing()) {
                            mPopupWindow.dismiss();
                            mPopupWindow = null;
                        }
                        return false;
                    }
                });
            }
        });

        good.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                good.setVisibility(View.INVISIBLE);
                goodChecked.setVisibility(View.VISIBLE);
                mGoodView.setImage(getResources().getDrawable(R.mipmap.video_third_goodup));
                mGoodView.show(v);
                bad.setEnabled(false);
            }
        });
        goodChecked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                good.setVisibility(View.VISIBLE);
                goodChecked.setVisibility(View.INVISIBLE);
                bad.setEnabled(true);
            }
        });
        bad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bad.setVisibility(View.INVISIBLE);
                badChecked.setVisibility(View.VISIBLE);
                good.setEnabled(false);
            }
        });
        badChecked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bad.setVisibility(View.VISIBLE);
                badChecked.setVisibility(View.INVISIBLE);
                good.setEnabled(true);
            }
        });
    }


}
